package com.demoo.zjsu.service.smartwebsite;

import com.demoo.zjsu.dao.smartwebsite.SmartWebDAO;
import com.demoo.zjsu.model.smartwebsite.PageTwo;
import com.demoo.zjsu.model.smartwebsite.PageTwoContent;
import com.demoo.zjsu.util.smartwebsite.WebsiteUtil;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jshjsh on 2016/8/16.
 * 对第二类页面适用（1对多的链接页面）
 */
@Service
public abstract class PageFactory extends IFactory{

    Logger logger = Logger.getLogger(getClass());

    @Autowired
    SmartWebDAO dao;

    public void resolver() {
        Page page = getPage();
        String category = page.getCategory();
        String url = page.getUrl();
        Document document = WebsiteUtil.getDocByURL(url);
        Elements main = getMain(document);
        filterElse(main);
        dealPhotoAndFile(main);
        List<PageTwo> pageTwos = new ArrayList<PageTwo>();
        List<PageTwoContent> contents = new ArrayList<PageTwoContent>();
        packageContent(main, pageTwos, contents, category);
        dao.insertListPageTwo(pageTwos);
        dao.insertListPageTwoContent(contents);
    }

    public void packageContent(Elements main, List<PageTwo> pageTwos, List<PageTwoContent> contents,String category) {
        Elements a = main.select("a");
        int size = a.size();
        for (int i = 0; i < size; i++) {
            Elements aElement = a.eq(i);
            String abs_url = aElement.attr("abs:href");
            String url = aElement.attr("href");
            String title = aElement.text();
            Document document1 = WebsiteUtil.getDocByURL(abs_url);
            PageTwo pageTwo = dealTitle(url, title);
            pageTwo.setCategory(category);
            String key = pageTwo.getKey();
            Elements elements = getOtherMain(document1);
            filterAgain(elements);
            filterMain(elements);
            dealPhotoAndFile(elements);
            PageTwoContent content = getContent(elements);
            content.setKey(key);
            if (content.getContent().equals(""))
                continue;
            contents.add(content);
            pageTwos.add(pageTwo);
        }
    }

    public abstract Elements getOtherMain(Document document);


    /**
     * 需要填充4个属性，分别是key，title，date,还有top
     * @param url
     * @param title
     *
     * @return
     */
    public abstract PageTwo dealTitle(String url, String title);
    /**
     * 在获得content后执行
     * @param elements
     */
    public abstract void filterAgain(Elements elements);

    /**
     * 需要填充三个属性，abbr，throat，content
     * @param elements
     * @return
     *
     * 可以重写。
     */

    public abstract PageTwoContent getContent(Elements elements);

    /**
     * 填充完成了content以及abbr，但只是添加了table的外衣,使用前请检查一下是否合适
     * @param elements
     * @return
     */
    public PageTwoContent get(Elements elements) {
        PageTwoContent content = new PageTwoContent();
        String con = "<table><tbody>" + elements.toString() + "</tbody></table>";
        content.setContent(con);
        return content;
    }

    /**
     * 这个方法适合一些简单删除的方法
     * @param elements
     * @return
     */
    public PageTwoContent getPageTwoContent(Elements elements) {
        String content = elements.toString();
        return new PageTwoContent(content, "");
    }

    /**
     * 这个方法适合一些简单删除的方法
     * @param elements
     * @return
     */
    public PageTwoContent getPageTwoContent(Elements elements, String s) {
        String content = elements.toString().replaceAll(s, "");
        String abbr = elements.text();
        return new PageTwoContent(content, "");
    }

    @Override
    public void filterElse(Elements elements) {

    }

    @Override
    public void addPhoto(Elements elements) {

    }

    @Override
    public void addFile(Elements elements) {

    }
}
